<?php 
namespace Admin\Controller;

use Admin\Model\Goods_classModel;

class AttributeController extends CommonController{

    public function index(){

        $list = M('attribute_name a')->field('a.*,b.name')->join('left join agency_goods_class b on a.class_id = b.class_id')->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function add(){

        if(IS_POST){
            $date = I('post.');

            $rel = M('attribute_name')->add($date);
            if($rel){
                die(json_encode(array('state'=>'200','rel'=>'成功！')));
            }


        }


        $cateM = new Goods_classModel();

        $cate = $cateM->getCateTree();

        $this->assign('cate',$cate);
        $this->display();
    }

    public function edit(){

        if(IS_POST){
            $data = I('post.');

            $map['attribute_name'] = $data['attribute_name'];
            $map['class_id'] = $data['class_id'];
            $map['state'] = $data['state'];
            $map['sort'] = $data['sort'];
            $map['memo'] = $data['memo'];

            $rel = M('attribute_name')->where(array('attribute_name_id'=>$data['save_id']))->save($map);

            if($rel){
                // header("location: http://www.dzshop.net/Admin/Index/index.html");
                $this->success("修改成功！",U("Attribute/index"),1);
            }else{
                $this->error("修改失败！");
            }


        }


        $id = I('get.att_id');

        $data =  M('attribute_name')->where(array('attribute_name_id'=>$id))->find();

        $ca = M('goods_class')->where(array('class_id'=>$data['class_id']))->find();
       $this->assign('ca',$ca);

        $cateM = new Goods_classModel();

        $cate = $cateM->getCateTree();

        $this->assign('cate',$cate);
        $this->assign('info',$data);
        $this->display();
    }

    public function dels(){
        $id = I('get.att_id');
       $map['state'] = 0;
        $rel = M('attribute_name')->where(array('attribute_name_id'=>$id))->save($map);
        if($rel){
            // header("location: http://www.dzshop.net/Admin/Index/index.html");
            $this->success("删除成功！",U("Attribute/index"),1);
        }else{
            $this->error("删除失败！");
        }
    }

    public function index_value(){

        $list = M('attribute_value a')->field('a.*,b.attribute_name')
            ->join('left join agency_attribute_name b on a.attribute_name_id = b.attribute_name_id')->select();

        $this->assign('list',$list);
        $this->display();
    }

    public function add_value(){

        if(IS_POST){
            $data = I('post.');
            $rel = M('attribute_value')->add($data);
            if($rel){
                die(json_encode(array('state'=>'200','rel'=>'成功！')));
            }

        }

        $list = M('attribute_name ')->field('attribute_name_id,attribute_name')->select();

        $this->assign('list',$list);
        $this->display();
    }

    public function value_edit(){

        if(IS_POST){
            $data = I('post.');
            $map['attribute_name_id'] = $data['attribute_name_id'];
            $map['value'] = $data['value'];
            $map['state'] = $data['state'];
            $map['sort'] = $data['sort'];
            $map['memo'] = $data['memo'];

            $rel = M('attribute_value')->where(array('attribute_id'=>$data['save_id']))->save($map);
            if($rel){
                // header("location: http://www.dzshop.net/Admin/Index/index.html");
                $this->success("修改成功！",U("Attribute/index_value"),1);
            }else{
                $this->error("删修改失败！");
            }
        }


        $id =I('get.att_id');
        $rel = M('attribute_value a')->field('a.*,b.attribute_name_id,b.attribute_name')->join('left join agency_attribute_name b on a.attribute_name_id=b.attribute_name_id')
            ->where(array('a.attribute_id'=>$id))->find();

        $list = M('attribute_name ')->field('attribute_name_id,attribute_name')->select();

        $this->assign('list',$list);
        $this->assign('rel',$rel);
        $this->display();
    }

    public function value_dels(){
      $id =I('get.att_id');
        $map['state']=0;
        $rel = M('attribute_value')->where(array('attribute_id'=>$id))->save($map);
        if($rel){
            // header("location: http://www.dzshop.net/Admin/Index/index.html");
            $this->success("删除成功！",U("Attribute/index_value"),1);
        }else{
            $this->error("删除失败！");
        }

    }

	/*protected function model($modelName){
		if(!$this->_model instanceof $modelName){
			$array=explode('M', $modelName);
			$this->_model=D($array[0]);
		}
		return $this->_model;
	}

    public function aaa(){
        dump($this->model(AttributeModel));
        echo "<hr>";
        dump($this->model(AttributeModel));
        echo "<hr>";
        dump(D('Attribute'));
        echo "<hr>";
        dump(D('Attribute'));
    }
	public function add(){
		if(IS_GET){
			//获取到所有的类型信息
			$type = D('Type')->select();
			$this->assign('type',$type);
			$this->display();
		}else{
			$data = $this->model(AttributeModel)->create();
			if(!$data){
				$this->error($this->model(AttributeModel)->getError());
			}
			$this->model(AttributeModel)->add($data);
			$this->success('写入成功');
		}
	}

    public function index(){
        $data = $this->model(AttributeModel)->listData();
        $this->assign("data",$data);
        $this->display();
    }

    public function edit(){
        if(IS_GET){
        $attr_id=intval(I('get.attr_id'));
        $info = $this->model(AttributeModel)->findOneById($attr_id);
        $this->assign("info",$info);
            //获取所有的类型信息
        $type=D('Type')->select();
        $this->assign("type",$type);
        $this->display();
        }else{
            $data = $this->model(AttributeModel)->create();
            if(!$data){
                $this->error($this->model(AttributeModel)->getError());
            }
            if($data['id']<=0){
                $this->error('参数错误');
            }
            $this->model(AttributeModel)->save($data);
            $this->success('修改成功',U('index'));
        }
    }

    public function dels(){
        $attr_id = intval(I('get.attr_id'));
        if($attr_id<=0){
            $this->error('参数错误');
        }
        $res = $this->model(AttributeModel)->remove($attr_id);
        if($res===false){
            $this->error('删除成功');
        }
        $this->success('删除失败');
    }*/
}